package com.headwind.logistics.permission.service.impl;

import com.headwind.logistics.permission.dto.MenuRoleDTO;
import com.headwind.logistics.permission.mapper.MenuMapper;
import com.headwind.logistics.common.pojo.Menu;
import com.headwind.logistics.permission.service.intl.MenuServiceIntl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单业务逻辑层实现类
 */
@Service
public class MenuServiceImpl implements MenuServiceIntl {
    //菜单数据访问层
    @Resource
    private MenuMapper menuMapper;

    /**
     * 获取菜单 角色
     * @param page
     * @param rows
     * @return
     */
    @Override
    public List<MenuRoleDTO> queryMenuRolePage(Integer page, Integer rows) {
        List<MenuRoleDTO> menuRoleDTOS = menuMapper.queryMenuRolePage(page, rows);

        return menuRoleDTOS;
    }

    /**
     * 获取菜单 角色  使用result方式
     * @param page
     * @param rows
     * @return
     */
    @Override
    public List<Menu> queryMenuRolePageMap(Integer page, Integer rows) {
        return menuMapper.queryMenuRolePageMap(page,rows);
    }

    /**
     * 添加角色菜单关联表
     * @param menuId
     * @param roleId
     * @return
     */
    @Override
    public int addMenuRole(Long menuId, Long roleId) {


        return menuMapper.addMenuRole(menuId, roleId);
    }

    /**
     * 删除角色菜单关联表
     * @param menuId
     * @param roleId
     * @return
     */
    @Override
    public int deleteMenuRole(Long menuId, Long roleId) {
        return menuMapper.deleteMenuRole(menuId, roleId);
    }

    /**
     * 根据菜单Id删除菜单
     * @param menuId
     * @return
     */
    @Override
    @Transactional
    public Integer deleteMenuByMenuId(Long menuId) {
        //删除菜单
        menuMapper.deleteMenuByMenuId(menuId);
        //删除菜单角色管理表
        int count = menuMapper.deleteMenuRoleByMenuId(menuId);

        return count;
    }

    /**
     * 获取全部菜单
     * @return
     */
    @Override
    public List<Menu> queryAllMenu() {

        return menuMapper.queryAllMenu();
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @Override
    @Transactional
    public Integer addMenu(Menu menu,String roleIdList) {

        //添加菜单表
        int count = menuMapper.addMenu(menu);

        if (count > 0){
            String[] ids = roleIdList.split("-");
            for (String id : ids) {
                menuMapper.addMenuRole(menu.getMenuId(),Long.parseLong(id));
            }

        }

        return count;
    }


}
